const keys = require("./mongodb");
const mongoose = require('mongoose');
const User = mongoose.model("users")
const JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;

const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;

// 配置passport
module.exports = passport => {
    passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
        User.findOne({ '_id': jwt_payload.id, 'isvalid': false }).then((user) => {
            if (user) {
                return done(null, user)
            }
            return done(null, false)
        }).catch(err => console.log(err))
    }));
}